export const random = (min, max) => {
	return min + Math.random() * (max - min);
};
export const randomInt = (min, max) => {
	return Math.floor(min + Math.random() * (max + 1 - min));
};
export const isFloat = num => {
	return ~~num !== num;
};
export const chineseNumber = num => {
	//将阿拉伯数字翻译成中文的大写数字
	const AA = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
	const BB = ['', '十', '百', '仟', '萬', '億', '点', ''];
	const a = ('' + num).replace(/(^0*)/g, '').split('.');
	let k = 0;
	let result = '';
	for (let i = a[0].length - 1; i >= 0; i--) {
		// eslint-disable-next-line default-case
		switch (k) {
			case 0:
				result = BB[7] + result;
				break;
			case 4:
				if (!new RegExp('0{4}//d{' + (a[0].length - i - 1) + '}$').test(a[0])) {
					result = BB[4] + result;
				}
				break;
			case 8:
				result = BB[5] + result;
				BB[7] = BB[5];
				k = 0;
				break;
		}
		if (
			k % 4 === 2 &&
			Number(a[0].charAt(i + 2)) !== 0 &&
			Number(a[0].charAt(i + 1)) === 0
		) {
			result = AA[0] + result;
		}
		if (Number(a[0].charAt(i)) !== 0) {
			result = AA[a[0].charAt(i)] + BB[k % 4] + result;
		}
		k++;
	}
	if (a.length > 1) {
		// 加上小数部分(如果有小数部分)
		result += BB[6];
		for (let i = 0; i < a[1].length; i++) {
			result += AA[a[1].charAt(i)];
		}
	}
	if (result === '一十') {
		result = '十';
	}
	if (result.match(/^一/) && result.length === 3) {
		result = result.replace('一', '');
	}
	return result;
};
export const chineseMoney = num => {
	const part = String(num).split('.');
	let newchar = '';
	//小数点前进行转化
	for (let i = part[0].length - 1; i >= 0; i--) {
		if (part[0].length > 10) {
			return '';
			//若数量超过拾亿单位，提示
		}
		let tmpnewchar = '';
		const perchar = part[0].charAt(i);
		switch (perchar) {
			case '0':
				tmpnewchar = '零' + tmpnewchar;
				break;
			case '1':
				tmpnewchar = '壹' + tmpnewchar;
				break;
			case '2':
				tmpnewchar = '贰' + tmpnewchar;
				break;
			case '3':
				tmpnewchar = '叁' + tmpnewchar;
				break;
			case '4':
				tmpnewchar = '肆' + tmpnewchar;
				break;
			case '5':
				tmpnewchar = '伍' + tmpnewchar;
				break;
			case '6':
				tmpnewchar = '陆' + tmpnewchar;
				break;
			case '7':
				tmpnewchar = '柒' + tmpnewchar;
				break;
			case '8':
				tmpnewchar = '捌' + tmpnewchar;
				break;
			case '9':
				tmpnewchar = '玖' + tmpnewchar;
				break;
			default:
				throw new Error('无法转换');
		}
		switch (part[0].length - i - 1) {
			case 0:
				tmpnewchar = tmpnewchar + '元';
				break;
			case 1:
				if (perchar !== '0') {
					tmpnewchar = tmpnewchar + '拾';
				}
				break;
			case 2:
				if (perchar !== '0') {
					tmpnewchar = tmpnewchar + '佰';
				}
				break;
			case 3:
				if (perchar !== '0') {
					tmpnewchar = tmpnewchar + '仟';
				}
				break;
			case 4:
				tmpnewchar = tmpnewchar + '万';
				break;
			case 5:
				if (perchar !== '0') {
					tmpnewchar = tmpnewchar + '拾';
				}
				break;
			case 6:
				if (perchar !== '0') {
					tmpnewchar = tmpnewchar + '佰';
				}
				break;
			case 7:
				if (perchar !== '0') {
					tmpnewchar = tmpnewchar + '仟';
				}
				break;
			case 8:
				tmpnewchar = tmpnewchar + '亿';
				break;
			case 9:
				tmpnewchar = tmpnewchar + '拾';
				break;
			default:
				throw new Error('无法转换');
		}
		newchar = tmpnewchar + newchar;
	}
	//小数点之后进行转化
	if (isFloat(num)) {
		if (part[1].length > 2) {
			// 小数点之后只保留两位
			part[1] = part[1].substr(0, 2);
		}
		for (let i = 0; i < part[1].length; i++) {
			let tmpnewchar = '';
			const perchar = part[1].charAt(i);
			switch (perchar) {
				case '0':
					tmpnewchar = '零' + tmpnewchar;
					break;
				case '1':
					tmpnewchar = '壹' + tmpnewchar;
					break;
				case '2':
					tmpnewchar = '贰' + tmpnewchar;
					break;
				case '3':
					tmpnewchar = '叁' + tmpnewchar;
					break;
				case '4':
					tmpnewchar = '肆' + tmpnewchar;
					break;
				case '5':
					tmpnewchar = '伍' + tmpnewchar;
					break;
				case '6':
					tmpnewchar = '陆' + tmpnewchar;
					break;
				case '7':
					tmpnewchar = '柒' + tmpnewchar;
					break;
				case '8':
					tmpnewchar = '捌' + tmpnewchar;
					break;
				case '9':
					tmpnewchar = '玖' + tmpnewchar;
					break;
				default:
					throw new Error('无法转换');
			}
			if (i === 0) {
				tmpnewchar = tmpnewchar + '角';
			}
			if (i === 1) {
				tmpnewchar = tmpnewchar + '分';
			}
			newchar = newchar + tmpnewchar;
		}
	}
	//替换所有无用汉字
	while (newchar.includes('零零')) {
		newchar = newchar.replace('零零', '零');
	}
	newchar = newchar.replace('零亿', '亿');
	newchar = newchar.replace('亿万', '亿');
	newchar = newchar.replace('零万', '万');
	newchar = newchar.replace('零元', '元');
	newchar = newchar.replace('零角', '');
	newchar = newchar.replace('零分', '');
	if (newchar.charAt(newchar.length - 1) === '元') {
		newchar = newchar + '整';
	}
	return newchar;
};
